import { login, logout, getInfo, loginZhang, weiLogin, zlogin, banglogin } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { Message } from 'element-ui'


const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    phone: "",
    roles: [],
    userId: null,
    permissions: [],
    examineFailReason: "",
    examineStatus: "", //1已通过  2未通过 3待提交认证  这俩不用了
    info: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
    },
    SET_NICKNAME: (state, nickName) => {
      state.nickName = nickName
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USERID: (state, userid) => {
      state.userId = userid
    },
    SET_USERINFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    blogin({ commit }, userInfo) {
      const phoneNumber = userInfo.phoneNumber
      const sendType = userInfo.sendType
      const code = userInfo.code
      const uuid = userInfo.uuid
      const smsCode = userInfo.smsCode
      const registeredType = userInfo.registeredType
      const openId = userInfo.openId
      return new Promise((resolve, reject) => {
        banglogin(phoneNumber, sendType, code, uuid, smsCode, registeredType, openId).then(res => {//重写登录
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
          console.log(res, "store")
        }).catch(error => {
          reject(error)
          console.log(error, "storeree")

        })
      })
    },
    zhuLogin({ commit }, userInfo) {
      const phoneNumber = userInfo.phoneNumber
      const sendType = userInfo.sendType
      const code = userInfo.code
      const uuid = userInfo.uuid
      const smsCode = userInfo.smsCode
      const registeredType = userInfo.registeredType
      const openId = userInfo.openId
      return new Promise((resolve, reject) => {
        zlogin(phoneNumber, sendType, code, uuid, smsCode, registeredType, openId).then(res => {//重写登录
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
          console.log(res, "store")
        }).catch(error => {
          reject(error)
          console.log(error, "storeree")

        })
      })
    },
    // 登录
    Login({ commit }, userInfo) {
      const phoneNumber = userInfo.phoneNumber
      const sendType = userInfo.sendType
      const code = userInfo.code
      const uuid = userInfo.uuid
      const smsCode = userInfo.smsCode
      return new Promise((resolve, reject) => {
        login(phoneNumber, sendType, code, uuid, smsCode).then(res => {//重写登录
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
          console.log(res, "store")
        }).catch(error => {
          reject(error)
          console.log(error, "storeree")

        })
      })
    },
    wLogin({ commit }, userInfo) {
      const phoneNumber = userInfo.phoneNumber
      const sendType = userInfo.sendType
      const code = userInfo.code
      const uuid = userInfo.uuid
      const smsCode = userInfo.smsCode
      const openId = userInfo.openId
      return new Promise((resolve, reject) => {
        weiLogin(phoneNumber, sendType, code, uuid, smsCode, openId).then(res => {//重写登录
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
          console.log(res, "store")
        }).catch(error => {
          reject(error)
          console.log(error, "storeree")

        })
      })
    },


    //新登录
    NewLogin({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        loginZhang(username, password, code, uuid).then(res => {//重写登录
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    userRole({ commit, state }) {
      switch (state.roles[0]) {
        case 'contentUser':
          return '医普号'
        case 'examine':
          return '审核人员'
        case 'operateUser':
          return '运营用户'
        case 'ordinaryUser':
          return '普通用户'
        case 'newMediaUser':
          return '新媒体'
      }
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          commit('SET_PHONE', user.phonenumber)
          commit('SET_NICKNAME', user.nickName)
          commit('SET_USERID', user.userId)
          commit('SET_USERINFO', user)
          // console.log(user.examineStatus);
          // if(user.examineStatus !== "1" && user.examineStatus !==undefined){
          //0 待审核 1已通过  2未通过 3待提交认证
          //console.log(user.examineStatus);
          if (user.examineFailReason && user.examineStatus.toString() == '2') {
            Message({
              message: user?.examineFailReason,
              type: 'error',
              duration: 8000,
              showClose: true
            })
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
